const myPlugins = store => {
    store.subscribe((mutation) => {
        console.log(mutation.type)
    //   if (mutation.type.startsWith('cart/')) {
    //     window.localStorage.setItem('cart-products', JSON.stringify(state.cart.cartProducts))
    //   }
    })
  }
export default {
    namespaced: true,
    state: {
        cartProducts: JSON.parse(window.localStorage.getItem('cart-products')) || []
    },
    getters: {
        // 商品种类数量
        count(state) {
            return state.cartProducts.length
        },
        // 购物车商品总数量
        totalCount(state) {
            return state.cartProducts.reduce((a, b) => {
                return a + b.count
            }, 0)
        },
        // 购物车商品总价格
        totalPrice(state) {
            return Number(state.cartProducts.reduce((a, b) => {
                return a + b.count * b.price
            }, 0).toFixed(2))
        },
        // 已选中商品的总数量
        checkedTotalCount(state) {
            return state.cartProducts.reduce((a, b) => {
                if (b.isChecked) {
                    a += b.count
                }
                return a
            }, 0)
        },
        // 已选中商品的总价
        checkedTotalPrice(state) {
            return Number((state.cartProducts.reduce((a, b) => {
                if (b.isChecked) {
                    a += b.totalPrice
                }
                return a
            }, 0)).toFixed(2))
        }
    },
    mutations: {
        // 添加购物车商品
        addCart(state, product) {
            product.isChecked = true
            let cartProduct = state.cartProducts.find(item => item.id === product.id)
            if (!cartProduct) {
                state.cartProducts.push({
                    ...product,
                    count: 1,
                    totalPrice: product.price
                })
            } else {
                cartProduct.count++
                cartProduct.totalPrice = Number((cartProduct.price * cartProduct.count).toFixed(2))
            }
        },
        // 修改购物车商品数量
        setProductCount(state, {
            productId,
            count
        }) {
            let cartProduct = state.cartProducts.find(item => item.id === productId)
            cartProduct.count = count
            cartProduct.totalPrice = Number((cartProduct.price * count).toFixed(2))
        },
        // 删除购物车商品
        deleteCart(state, product) {
            let index = state.cartProducts.findIndex(item => product.id === item.id) //state.cartProducts.find((item) => item.id === product.id)
            state.cartProducts.splice(index, 1)
        },
        // 全选购物车商品
        chooseAllProducts(state, checked) {
            state.cartProducts.forEach(product => {
                product.isChecked = checked
            })
        },
        // 选择购物车商品
        chooseProduct(state, {
            checked,
            productId
        }) {
            const product = state.cartProducts.find(item => item.id === productId)
            product && (product.isChecked = checked)
        }
    },
    actions: {

    },
    plugins:[myPlugins]
}